package cn.com.server.service.impl;

import cn.com.server.dao.RoleResourceDao;
import cn.com.server.entity.RoleResource;
import cn.com.server.service.RoleResourceService;

import java.sql.SQLDataException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 角色资源绑定表 服务实现类
 * </p>
 *
 * @author qing
 * @since 2017-11-20
 */
@Service
public class RoleResourceServiceImp implements RoleResourceService {
	@Autowired 
	private RoleResourceDao dao;
	
	@Override
	public int insert(RoleResource entity) {
		return dao.insert(entity);
	}
	
	@Override
	public int update(RoleResource entity) {
		return dao.update(entity);
	}
	
	@Override
	public int updateByRoleId(Long roleId) {
		return dao.updateByRoleId(roleId);
	}
	
	@Override
	@Transactional
	public int insertAndUpdateRoleResource(String resourceIds,Long roleId) throws SQLDataException {
		try {
			dao.updateByRoleId(roleId);
			String[] resourceIdArr = resourceIds.split(",");
			for(String id : resourceIdArr) {
				if(id!=null&&!"".equals(id)) {
					RoleResource entity = new RoleResource();
					entity.setRoleId(roleId);
					entity.setResourceId(Long.valueOf(id));
					dao.insert(entity);
				}
			}
			return 1;
		}catch (Exception e) {
			e.printStackTrace();
			throw new SQLDataException();
		}
	}
	
	@Override
	public List<RoleResource> selectRoleResourceListByRoleId(Long roleId){
		return dao.selectRoleResourceListByRoleId(roleId);
	}
}
